//
//  HotListenListViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "HotListenListViewController.h"
#import "ManyListenListTableViewCell.h"
#import "CustomerNetworking.h"
#import "HotListenListModel.h"
#import "MoreNewSongsModel.h"
#import "HotListenListHeaderView.h"
#import "NavigationBarBackImageView.h"
#import "PlayerViewController.h"
#import "MusicType.h"
#import "SongsAuditionListModel.h"
@interface HotListenListViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) HotListenListModel *hotListenListModel;
@property(nonatomic, retain) MoreNewSongsModel *moreNewSongsModel;
@property(nonatomic, retain) NSMutableArray *songArray;
@property(nonatomic, retain) HotListenListHeaderView *headerView;
@property(nonatomic, assign) CGFloat contentOffSety;
@property(nonatomic, retain) UINavigationBar *bar;
@property(nonatomic, retain) NavigationBarBackImageView *barBackImgeView;
@end
@implementation HotListenListViewController
- (void)dealloc {
    [_idString release];
    [_numberString release];
    [_tableView release];
    [_hotListenListModel release];
    [_moreNewSongsModel release];
    [_songArray release];
    [_headerView release];
    [_bar release];
    [_barBackImgeView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.songArray = [NSMutableArray array];
    //创建tableView
    [self createTableView];
    //获取数据
    [self loadData];
    //创建自定义navigationbar
    [self createTabbar];
}
#pragma mark 创建自定义navigationbar
- (void)createTabbar {
    self.bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 375, 64)];
        //navigationbar背景图片
    self.barBackImgeView = [[NavigationBarBackImageView alloc]initWithFrame:CGRectMake(0, 0, _bar.frame.size.width, _bar.frame.size.height)];
    _barBackImgeView.alpha = 0;
    [_bar addSubview:_barBackImgeView];
    [_barBackImgeView release];

    _bar.tintColor = [UIColor whiteColor];
    [_bar setBackgroundImage:[UIImage imageNamed:@"nav.jpg"] forBarMetrics:UIBarMetricsCompact];
    //去除tabar黑线
    [[self findHairlineImageViewUnder:_bar] setHidden:YES];
    UIBarButtonItem *itemBar = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(retutnAction:)];
    UINavigationItem *item = [[UINavigationItem alloc] init];
    [_bar pushNavigationItem:item animated:YES];
    [item setLeftBarButtonItem:itemBar];
    [self.view addSubview:_bar];
    [_bar release];
}
- (void)retutnAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
#pragma mark 获取数据
- (void)loadData {
  [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"%@%@?app=ttpod&v=v8.1.1.2015110616", self.numberString,self.idString] andParameters:nil andBlock:^(id response) {
      if ([self.numberString isEqualToString:@"http://api.dongting.com/song/album/"] || [self.numberString isEqualToString:@"http://api.dongting.com/song/album/"]) {
          self.moreNewSongsModel = [[MoreNewSongsModel alloc]initWithDictionary:[response valueForKey:@"data"]];
          for (ListenDataSongsModel *model in self.moreNewSongsModel.songList) {
              [self.songArray addObject:model];
              [model release];
          }
          _headerView.picString = _moreNewSongsModel.picUrl;
          if (![_moreNewSongsModel.favoriteCount isEqualToString:@"(null)"]) {
              _headerView.likeString = _moreNewSongsModel.favoriteCount;
          }
          _headerView.shareString = @"分享";
           _barBackImgeView.titleString = _moreNewSongsModel.name;
      }else {
      self.hotListenListModel = [[HotListenListModel alloc]initWithDictionary:response];
          for (ListenDataSongsModel *model in self.hotListenListModel.songs) {
              [self.songArray addObject:model];
              [model release];
          }
          _headerView.picString = [_hotListenListModel.image valueForKey:@"pic"];
          _headerView.shareString = _hotListenListModel.share_count;
          _headerView.nameStrirng = _hotListenListModel.title;
          _barBackImgeView.titleString = _hotListenListModel.title;
          if (![self.hotListenListModel.favorite_count isEqualToString:@"(null)"]) {
              _headerView.likeString = _hotListenListModel.favorite_count;
          }
      }
      [_tableView reloadData];
  }];
}
#pragma mark 创建tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.contentOffSety = _tableView.contentOffset.y;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellAccessoryNone;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    //头视图
    self.headerView = [[HotListenListHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
    _tableView.tableHeaderView = _headerView;
    //播放的点击事件
    [_headerView.listeView.showButton addTarget:self action:@selector(showButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //分享的点击事件
    [_headerView.listeView.enjoyButton addTarget:self action:@selector(enjoyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //喜欢的点击事件
    [_headerView.listeView.heartButton addTarget:self action:@selector(heartButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //注册cell的重用标识
    [_tableView registerClass:[ManyListenListTableViewCell class] forCellReuseIdentifier:@"ManyListenListViewControllerIdentify"];
    [self.view addSubview:_tableView];
    [_headerView release];
    [_tableView release];
}
#pragma mark cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ManyListenListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ManyListenListViewControllerIdentify" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    if (_songArray.count != 0) {
        cell.dataSongsModel = [_songArray objectAtIndex:indexPath.row];
    }
    cell.queueString = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return cell;
}
#pragma mark cell的大小
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height / 11;
}
#pragma mark cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (_songArray.count != 0) {
            return _songArray.count;
        }else {
            return 0;
        }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PlayerViewController *player = [PlayerViewController sharePlayerViewController];
    NSMutableArray *arr = [NSMutableArray array];
        ListenDataSongsModel *song = self.songArray[indexPath.row];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:song.albumName forKey:@"album"];// 专辑
        [dic setValue:song.name forKey:@"name"]; // 歌名
        NSMutableArray *array = [NSMutableArray array];
        for (SongsAuditionListModel *model in song.auditionList) {
            MusicType *type = [[MusicType alloc]init];
            type.url = model.urlString;
            type.typeDescription = model.typeDescription;
            [array addObject:type];
        }
        [dic setValue:array forKey:@"music"]; // 歌曲,model用musicType
        [dic setValue:song.singerName forKey:@"songerName"];// 歌手名
        [dic setValue:song.albumId forKey:@"id"]; // id
        [arr addObject:dic];
    player.musicArr = arr;
    [player MusicStart:0];
}
#pragma mark scrollview的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < -20)
    {
        CGFloat factor = ABS(scrollView.contentOffset.y) + 250 * KPropotionHeight - 20;
        CGRect f = CGRectMake(-([[UIScreen mainScreen] bounds].size.width*(ABS(scrollView.contentOffset.y) + 250 - 20)/250-[[UIScreen mainScreen] bounds].size.width)/2, -ABS(scrollView.contentOffset.y)+ 20, [[UIScreen mainScreen] bounds].size.width*(ABS(scrollView.contentOffset.y) + 250 - 20)/250, factor);
        self.headerView.picImageView.frame = f;
    } else {
        CGRect f = self.headerView.frame;
        f.origin.y = 0;
        self.headerView.frame = f;
        self.headerView.picImageView.frame = CGRectMake(0, f.origin.y, [[UIScreen mainScreen] bounds].size.width, 250 * KPropotionHeight);
    }
    if (scrollView == self.tableView) {
        self.headerView.listeView.backView.alpha = self.tableView.contentOffset.y / 100;
        if (self.headerView.listeView.backView.alpha > 1) {
            self.headerView.listeView.backView.alpha =  1;
        }else if (self.headerView.listeView.backView.alpha < 0.7) {
            self.headerView.listeView.backView.alpha =  0.7;
        }
        self.barBackImgeView.alpha = self.tableView.contentOffset.y / 100;
        if (self.tableView.contentOffset.y > 115 && self.tableView.contentOffset.y > self.contentOffSety) {
           self.tableView.frame = CGRectMake(0, -20, self.tableView.frame.size.width, self.tableView.frame.size.height);
            self.headerView.listeView.frame = CGRectMake(0, 64, self.headerView.listeView.frame.size.width, self.headerView.listeView.frame.size.height);
            [self.view addSubview:self.headerView.listeView];
        }else if ( self.tableView.contentOffset.y < 115 && self.tableView.contentOffset.y < self.contentOffSety){
            self.tableView.frame = CGRectMake(0, -20, self.tableView.frame.size.width, self.tableView.frame.size.height);
            self.headerView.listeView.frame = CGRectMake(0, self.tableView.tableHeaderView.frame.size.height - self.headerView.listeView.frame.size.height, self.headerView.listeView.frame.size.width, self.headerView.listeView.frame.size.height);
            [self.tableView.tableHeaderView addSubview:self.headerView.listeView];
        }
        self.contentOffSety = self.tableView.contentOffset.y;
    }
}
#pragma mark 全部播放的点击事件
- (void)showButtonAction:(UIButton *)button {
    PlayerViewController *player = [PlayerViewController sharePlayerViewController];
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.songArray.count; i++)
    {
        ListenDataSongsModel *song = self.songArray[i];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:song.albumName forKey:@"album"];// 专辑
        [dic setValue:song.name forKey:@"name"]; // 歌名
        NSMutableArray *array = [NSMutableArray array];
        for (SongsAuditionListModel *model in song.auditionList) {
            MusicType *type = [[MusicType alloc]init];
            type.url = model.urlString;
            type.typeDescription = model.typeDescription;
            [array addObject:type];
        }
        [dic setValue:array forKey:@"music"]; // 歌曲,model用musicType
        [dic setValue:song.singerName forKey:@"songerName"];// 歌手名
        [dic setValue:song.albumId forKey:@"id"]; // id
        [arr addObject:dic];
    }
    player.musicArr = arr;
    [player MusicStart:0];

}
#pragma mark 分享的点击事件
- (void)enjoyButtonAction:(UIButton *)button {
    NSLog(@"分享");
}
#pragma mark 收藏的点击事件
- (void)heartButtonAction:(UIButton *)button {
    NSLog(@"收藏");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
